import { Button } from 'antd'
import React from 'react'
import { isAuth } from '../../helper/auth'
import { Link } from 'react-router-dom'
import axios from 'axios'
import { API } from '../../config'
import { emptyCart } from '../../helper/cart'

export default function CarBuy({
	totalMount,
	product,
	address,
}) {

	const id = product.map(item => item.id)
	const count = product.map(item => item.count)
	const {user} = isAuth()
	// 提交
	const handleSubmit = async () =>{
		await axios.post(`${API}/alipay`, {
			totalAmount: totalMount,
			subject: '测试标题',
			body: '测试订单描述',
			products: [{product: id, count}],
			address,
			useId: user._id
		}).then(res=>{
			window.location.href = res.data.result
			emptyCart()
		})
	}
	return (
		<>
			{
				 isAuth() ? 
					<Button onClick={handleSubmit}>提交</Button> : 
					<Button type='primary'>
						<Link to='/login'>登录</Link>		
					</Button>
			}
		</>
	)
}
